{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-09-10T11:27:13.791322Z",
     "start_time": "2025-09-10T11:27:13.777248Z"
    }
   },
   "source": [
    "from itertools import product\n",
    "import time;\n",
    "# 字典生成\n",
    "d1 = {'name': 'age', 'height': 'weight'}\n",
    "d2 = dict({'name':'age', 'height': 'weight'})\n",
    "d3 = dict([('name', 'age'), ('height', 'weight')])\n",
    "d4 = dict(name='age', height='weight')\n",
    "d1 == d2 == d3 == d4\n",
    "print(d1.get('location'))\n",
    "print('height' in d1)\n",
    "# 集合生成 (本质上是一个hash表）\n",
    "s1 = {1,2,3}\n",
    "s2 = set([1,2,3,7,2])\n",
    "s2.add(4)\n",
    "# s2.remove(4)\n",
    "# 集合排序\n",
    "print(sorted(s2))\n",
    "s1 == s2\n",
    "d = {'c': 10, 'a': 20, 'b': 30}\n",
    "# 根据字典的升序排列\n",
    "print(sorted(d.items(), key=lambda x:x[0]))\n",
    "# 根据字典的降序排列\n",
    "print(sorted(d.items(), key=lambda x:x[1]))\n",
    "\n",
    "# 使用列表存储数据结构\n",
    "def find_product_price(product,product_id):\n",
    "    for id,price in product:\n",
    "        if id == product_id:\n",
    "           return price\n",
    "    return None\n",
    "products = [(143121312, 100),\n",
    "    (432314553, 30),\n",
    "    (32421912367, 150)]\n",
    "print(find_product_price(products,4323144553))\n",
    "products1 = {\n",
    "  143121312: 100,\n",
    "  432314553: 30,\n",
    "  32421912367: 150\n",
    "}\n",
    "print(products1[432314553])\n",
    "\n",
    "# 找出商品有多少种不同的价格（列表）\n",
    "def find_unique_price_using_list(products2):\n",
    "    unique_price_list = []\n",
    "    for _, price in products2:\n",
    "        if price not in unique_price_list:\n",
    "            unique_price_list.append(price)\n",
    "    return len(unique_price_list)\n",
    "products2 = [\n",
    "    (143121312, 100),\n",
    "    (432314553, 30),\n",
    "    (32421912367, 150),\n",
    "    (937153201, 30)\n",
    "]\n",
    "print(find_unique_price_using_list(products2))\n",
    "\n",
    "# 找出商品有多少种不同的价格（集合）集合的key值是唯一的\n",
    "def find_unique_price_using_set(products3):\n",
    "    unique_price_set = set()\n",
    "    for _, price in products2:\n",
    "            unique_price_set.add(price)\n",
    "    return len(unique_price_set)\n",
    "products3 = [\n",
    "    (143121312, 100),\n",
    "    (432314553, 30),\n",
    "    (32421912367, 150),\n",
    "    (937153201, 30)\n",
    "]\n",
    "print(find_unique_price_using_set(products3))\n",
    "\n",
    "id = [x for x in range(1,100000)]\n",
    "price = [x for x in range(200000, 300000)]\n",
    "products_new = list(zip(id, price))\n",
    "# 计算运行时间\n",
    "start_using_list = time.perf_counter()\n",
    "# find_unique_price_using_list(products_new)\n",
    "print(start_using_list)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n",
      "True\n",
      "[1, 2, 3, 4, 7]\n",
      "[('a', 20), ('b', 30), ('c', 10)]\n",
      "[('c', 10), ('a', 20), ('b', 30)]\n",
      "None\n",
      "30\n",
      "3\n",
      "3\n",
      "519578.5319935\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "ccc04ae8e9bae2f2"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
